package com.ttai.ui.activity;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.igexin.sdk.PushManager;
import com.suke.widget.SwitchButton;
import com.ttai.R;
import com.ttai.dagger.componet.activity.DaggerRegisterComponet;
import com.ttai.dagger.module.activity.RegisterPresenterModule;
import com.ttai.presenter.activity.RegisterPresenter;
import com.ttai.presenter.activity.SigninPresenter;
import com.ttai.ui.base.BaseActivity;
import com.ttai.untils.Constant;

import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;

import javax.inject.Inject;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class Register extends BaseActivity {
    @Bind(R.id.et_registerphone)
    EditText etRegisterphone;
    @Bind(R.id.et_registercode)
    EditText etRegistercode;
    @Bind(R.id.btn_resisterget)
    Button btnResisterget;
    @Bind(R.id.et_registeremail)
    EditText etRegisteremail;
    @Bind(R.id.switchButton)
    SwitchButton switchButton;
    @Bind(R.id.btn_finsh)
    Button btnFinsh;

    @Inject
    RegisterPresenter registerPresenter;

    SigninPresenter signinPresenter;

    public static String phonenumber="";

    /**
     * 计时器
     */
    private CountDownTimer timer = new CountDownTimer(30000, 1000) {

        @Override
        public void onTick(long millisUntilFinished) {
            btnResisterget.setEnabled(false);
            btnResisterget.setText("休息"+(millisUntilFinished / 1000) + "秒");
        }

        @Override
        public void onFinish() {
            btnResisterget.setEnabled(true);
            btnResisterget.setText("获取验证码");
        }
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        ButterKnife.bind(this);

        signinPresenter = new SigninPresenter(this);
        setComponet();
    }

    @Override
    public void setComponet() {
        DaggerRegisterComponet.builder()
                .registerPresenterModule(new RegisterPresenterModule(this))
                .build()
                .in(this);

    }

    @Override
    public void next(View view) {

    }

    @Override
    public void pre(View view) {
        finish();
    }


    @OnClick({R.id.btn_resisterget, R.id.btn_finsh})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_resisterget:
                phonenumber=etRegisterphone.getText().toString();
                /**
                 * 判断是否为手机号
                 */

                String mac= Constant.getMacAddress();

                if (!Constant.isMobile(phonenumber)){
                    Toast.makeText(Register.this,"输入的手机号无效",Toast.LENGTH_SHORT).show();
                }
                else {
                    /**
                     * 启动计时器
                     */
                    timer.start();
                }

                registerPresenter.getVerifyCode(phonenumber,"0");

                break;
            case R.id.btn_finsh:
                String scode = getScode(etRegistercode.getText().toString()+registerPresenter.buildTime.getBuildtime());
                String clientId = PushManager.getInstance().getClientid(this);
                String email = etRegisteremail.getText().toString();
                signinPresenter.register(scode,phonenumber,email,clientId);
                break;
        }
    }

    private String getScode(String s) {
        String scode = null;
        try {
            scode = Constant.mD5Code(s);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return scode;
    }
}
